package com.sgb.contract.view.ui.openaccount;

import android.content.Context;

import com.sgb.contract.api.ContractNet;
import com.sgb.contract.base.viewmodel.BaseViewModel;
import com.sgb.contract.test.model.OpenAccountDetailEntity;
import com.sgb.contract.test.model.OpenAccountEntity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.Map;

import androidx.lifecycle.MutableLiveData;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * AccountDetailViewModel
 *
 * @author chentao
 */
public class OpenAccountViewModel extends BaseViewModel {

    public MutableLiveData<Object> openAccount = new MutableLiveData<>();
    public MutableLiveData<Object> code = new MutableLiveData<>();
    public MutableLiveData<String> mCode = new MutableLiveData<>();
    public MutableLiveData<OpenAccountDetailEntity> detail = new MutableLiveData<>();
    public MutableLiveData<Object> error = new MutableLiveData<>();

    public void openAccount(Context context, OpenAccountEntity bean) {
        DialogHelper.showProgressDialog(context, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        ContractNet.getInstance().getContractApi().openAccount(bean).enqueue(new Callback<BaseEntity<Object>>() {
            @Override
            public void onResponse(Call<BaseEntity<Object>> call, Response<BaseEntity<Object>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    MToast.showToast(context, response.message());
                    return;
                }
                if ("ok".equals(response.body().getState())) {
                    openAccount.setValue("1");
                } else {
                    if ("10000".equals(response.body().getCode())) {
                        openAccount.setValue(response.body().getMsg());
                    } else if ("10002".equals(response.body().getCode())) {
                        error.setValue(response.body().getMsg());
                    } else {
                        MToast.showToast(context, response.body().getMsg());
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<Object>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(context, t.getMessage());
            }
        });
    }

    public void getCompanyDetail(Context context) {
        DialogHelper.showProgressDialog(context, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        ContractNet.getInstance().getContractApi().getCompanyDetail().enqueue(new Callback<BaseEntity<OpenAccountDetailEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<OpenAccountDetailEntity>> call, Response<BaseEntity<OpenAccountDetailEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.isSuccessful() && "ok".equals(response.body().getState())) {
                    detail.setValue(response.body().getData());
                } else {
                    MToast.showToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<OpenAccountDetailEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(context, t.getMessage());
            }
        });

    }

    public void getValidateCode(Context context, String tel) {
        DialogHelper.showProgressDialog(context, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        ContractNet.getInstance().getContractApi().getValidateCode(tel, false).enqueue(new Callback<BaseEntity<Object>>() {
            @Override
            public void onResponse(Call<BaseEntity<Object>> call, Response<BaseEntity<Object>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.isSuccessful() && "ok".equals(response.body().getState())) {
                    code.setValue(1);
                } else {
                    MToast.showToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<Object>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(context, t.getMessage());
            }
        });
    }


    public void personOpenAccount(Context context, Map map) {
        DialogHelper.showProgressDialog(context, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        ContractNet.getInstance().getContractApi().personOpenAccount(map).enqueue(new Callback<BaseEntity<Object>>() {
            @Override
            public void onResponse(Call<BaseEntity<Object>> call, Response<BaseEntity<Object>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.isSuccessful() && "ok".equals(response.body().getState())) {
                    showDialog("ok", "恭喜您，在线合同签约账户已开通");
                } else {
                    if (response.body().getCode().equals("539110")) {
                        showDialog("ok", "当前用户已开户，请勿重复开户！", "", "立即查看");
                    } else {
                        MToast.showToast(context, response.body().getMsg());
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<Object>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(context, t.getMessage());
            }
        });
    }

    public void updatePersonAccount(Context context, Map map) {
        DialogHelper.showProgressDialog(context, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        ContractNet.getInstance().getContractApi().updatePersonAccount(map).enqueue(new Callback<BaseEntity<Object>>() {
            @Override
            public void onResponse(Call<BaseEntity<Object>> call, Response<BaseEntity<Object>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.isSuccessful() && "ok".equals(response.body().getState())) {
                    showDialog("ok", "修改成功");
                } else {
                    MToast.showToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<Object>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(context, t.getMessage());
            }
        });
    }


}